package it.lupo.core;

import it.lupo.model.BarCode;
import it.lupo.model.User;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.log4j.Logger;

import com.google.common.collect.Maps;

/* singleton class */
public class LupoSession {

	private static final String SESSION_BARCODE = "session.barcode";
	private static final String SESSION_CONFERMENT_TYPE = "session.confermenttype";
	private static final String SESSION_USER = "session.userid";

	public static enum TYPE {
			PLASTIC,
			GLASS,
			PAPER,
			UNDIFFERENTIATED,
			ORGANIC,
			ALUMINIUM
			};

	private Map<String,Object> map = Maps.newHashMap();
	
	public static String version = "1.0";
	
	private static Logger log = Logger.getLogger(LupoSession.class);
	
	public final String START_SESSION = "lupo.session.start";
	
	private static LupoSession instance;
	
	private LupoSession(){
		Date now = new Date();
		map.put(START_SESSION, now);
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");		
		log.info("Lupo "+version+" started at "+sdf.format(now)+"!");
		
	}
	
	public static LupoSession getCurrent()
	{
		if(instance==null)
			instance=new LupoSession();
		return instance;
	}
	
	public void add(String key,Object value)
	{
		map.put(key,value);
	}
	
	public Object get(String key)
	{
		return map.get(key);
	}

	public void logout() {
		map.remove(SESSION_USER);
	}
	
	public void login(User username) {
		add(SESSION_USER,username);
	}
	
	public User getUser()
	{
		return (User)map.get(SESSION_USER);
	}
	
	public void setPlastic()
	{
		add(SESSION_CONFERMENT_TYPE,TYPE.PLASTIC);
	}
	public void setGlass()
	{
		add(SESSION_CONFERMENT_TYPE,TYPE.GLASS);
	}
	public void setPaper()
	{
		add(SESSION_CONFERMENT_TYPE,TYPE.PAPER);
	}

	public TYPE getType() {
		return (TYPE)get(SESSION_CONFERMENT_TYPE);
	}

	public void removeType() {
		map.remove(SESSION_CONFERMENT_TYPE);
	}

	public void setBarcodeSession(BarCode bcode) {
		add(SESSION_BARCODE,bcode);
	}
	
	public BarCode getBarcode() {
		return (BarCode)get(SESSION_BARCODE);
	}

	
}
